package cn.edu.nwsuaf.teachercenter.dao.impl;

import cn.edu.nwsuaf.teachercenter.dao.IBaseDao;
import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Repository(value = "baseDao")
public class IBaseDaoImpl<T> implements IBaseDao<T> {

	@Resource(name="sessionFactory")
	protected SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public T getById(Class<T> c, Serializable id) {
		Session session = null;
		T t = null;
		try{
			session = sessionFactory.getCurrentSession();
			t = (T)session.get(c, id);
		}catch(Exception e){
			e.printStackTrace();
		}
		return t;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public T loadById(Class<T>c, Serializable id){
		Session session = null;
		T t = null;
		try{
			session = sessionFactory.getCurrentSession();
			t = (T)session.load(c, id);
		}catch(Exception e){
			e.printStackTrace();
		}
		return t;
	}
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public List<T> findAll(Class<T> c) {
		Session session = null;
		List<T> resultList = null;
		
		try {
			session = sessionFactory.getCurrentSession();
			Query query = session.createQuery("FROM "+c.getName());
			
			resultList = query.list();
			
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		return resultList;
	}
	
	@Transactional(readOnly=true)
	public List<T> findByHql(String hql,String[] param){
		Session session = null;
		List<T> resultList = null;
		
		try {
			session = sessionFactory.getCurrentSession();
			Query query = session.createQuery(hql);	
			if(param != null && param.length > 0){
				for(int i = 0; i<param.length; i++){
					query.setString(i, param[i]);
				}
			}
			resultList = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public List<T> findDataByPage(String hql, String[] param, int currentpage, int rows) {
		Session session = null;
		List<T> resultList = null;
		
		try {
			session = sessionFactory.getCurrentSession();
			Query query = session.createQuery(hql);
			if(param != null && param.length > 0){
				for(int i = 0; i<param.length; i++){
					query.setString(i, param[i]);
				}
			}
			query.setFirstResult((currentpage-1)*rows).setMaxResults(rows);
			
			resultList = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}

	/**
	 * 本部分只能用统计查询语句
	 */
	@Override
	@Transactional(readOnly=true)
	public int countData(Class<T> c) {
		// TODO Auto-generated method stub
		Session session = null;
		int totalRows = 0;
		try {
			session = sessionFactory.getCurrentSession();
			Query query = session.createQuery("SELECT COUNT(*) FROM "+c.getName());
			
			if(query.uniqueResult()!=null) {
				totalRows = ((Long)query.uniqueResult()).intValue();
			}
		} catch(Exception e){
			e.printStackTrace();
		}
		return totalRows;
	}

	@Override
	@Transactional(readOnly=true)
	public int countData(String hql, String[] param) {
		// TODO Auto-generated method stub
		Session session = null;
		int totalRows = 0;
		try {
			session = sessionFactory.getCurrentSession();
			Query query = session.createQuery(hql);
			if(param != null && param.length > 0){
				for(int i = 0; i<param.length;i++){
					query.setString(i, param[i]);
				}
			}
			if(query.uniqueResult()!=null) {
				totalRows = ((Long)query.uniqueResult()).intValue();
			}
		} catch(Exception e){
			e.printStackTrace();
		}	
		return totalRows;
	}

	@Override
	@Transactional(readOnly=false)
	public void singleDataInsert(T t) {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
			session.save(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	@Transactional(readOnly=false)
	public void batchDataInsert(List<T> tList) {
		Session session = null;
		try {
			for(T t : tList){
				session.save(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	@Transactional(readOnly=false)
	public void singleDataDelete(T t) {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
			session.delete(t);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	@Transactional(readOnly=false)
	public void batchDataDelete(List<T> tList) {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
			for(T t : tList){
				session.delete(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	@Transactional(readOnly=false)
	public void singleDataUpdate(T t) {
		// TODO Auto-generated method stub
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
			session.merge(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	@Transactional(readOnly=false)
	public void batchDataUpdate(List<T> tList) {
		// TODO Auto-generated method stub
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
			for(T t : tList){
				session.merge(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

